package com.example.c94s3plyspringboot.web;

import com.example.c94s3plyspringboot.po.Result;
import com.example.c94s3plyspringboot.po.User;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("user")
public class UserAction {
    MultipartAutoConfiguration a;

    @RequestMapping("reg")
    public Result reg(@Valid User user, Errors errors){

        errors.getAllErrors();// 获取全部错误对象
        errors.getFieldErrors();// 获取字段验证错误
        errors.getGlobalErrors();// 获取全局错误
        errors.hasFieldErrors();// 获取是否有字段验证错误
        errors.hasGlobalErrors();
        errors.hasErrors();
        errors.reject("");  // 添加一个错误
        errors.rejectValue("",""); // 添加一个字段验证错误

        if(errors.hasErrors()){
            return new Result(0, "failure",
                    errors.getFieldErrors());
        }
        return new Result(1,"success", user);
    }

    @PostMapping("uploadHead")
    public String uploadHead(@RequestParam("head") MultipartFile file) throws IOException {
        // Servlet 3.0 Part
        String filename = file.getOriginalFilename(); // 获取文件名
        String name = file.getName();                 // 获取提交的字段名  head
            // <input type="file" name="head">
        file.transferTo(new File("d:/abc", filename));
        return "success";
    }



}
